import logo from '@resources/icons/logo.png';
import { App_Env_Enum, App_NODE_ENV_Enum, Win_Base_Func_Enum } from "@enum/app.enum";
import { Env } from "@utils/env";
import type { MenuItemConstructorOptions, MenuItem } from "electron";
import { app, Menu, Tray, nativeImage } from "electron";
import { Window } from '@main/service/base/window';

type TrayTypes = Array<MenuItemConstructorOptions | MenuItem>;

export interface TrayOptions {
  name?: string;
  iconPath?: string;
  menuTemplate?: TrayTypes;
}

const baseTrayItems: TrayTypes = [
  {
    label: "显示",
    click: () => Window.func(Win_Base_Func_Enum.SHOW)
  },
  {
    label: "退出",
    click: () => app.quit(),
  },
];

export const useTray = () => {
  const menuTemplateHandle = (items?: TrayTypes) => {
    return Menu.buildFromTemplate(baseTrayItems.concat(items ?? []));
  };

  const createTray = (opt?: TrayOptions) => {
    //等把资源处理做了再打开这个限制
    if (Env.getEnv(App_Env_Enum.NODE_ENV) !== App_NODE_ENV_Enum.PRODUCTION) {
        
      const tray = new Tray(
        nativeImage.createFromDataURL(
          opt?.iconPath ?? logo
        )
      );

      tray.setContextMenu(menuTemplateHandle(opt?.menuTemplate));

      tray.setToolTip(opt?.name ?? "Simple Electron");
    }
  };

  return {
    menuTemplateHandle,
    createTray,
  };
};
